*************************************************************************
*																		*
*					Campaign Panel Book Data Manipulation				*
*																		*
*************************************************************************	

/*
In this do-file those variables are manipulated which are necessary for a majority of the analyses
*/

cd "${data}"

clear mata
clear matrix
set more off
set scrollbufsize 2000000

*We use the matched dataset when manipulating the data
use "data2009_2013 participants 7 waves.dta", clear


*At first, we replace the negative values for 2013 by the GESIS standard

foreach var of varlist _all {
	capture confirm numeric var `var'
	if !_rc {
		mvdecode `var', mv(-71=.p \-72=.o \-81=.n \-82=.m \-83=.l ///
				 \-84=.k \-85=.j \-86=.i \-92=.h \-93=.g ///
				 \-94=.f \-95=.e \-96=.d \-97=.c \-98=.b ///
				 \-99=.a)
				 
	label define `var' ///
		.a"keine Angabe" ///
		.b"weiss nicht" ///
		.c"trifft nicht zu" ///
		.d"Split" ///
		.e"nicht teilgenommen" ///
		.f"nicht in Auswahlgesamtheit" ///
		.g"Interview abgebrochen" ///
		.h"Fehler in Daten" ///
		.i"nicht wahlberechtigt" ///
		.j"nicht waehlen" ///
		.k"keine Erst-/Zweitstimme abgegeben" ///
		.l"ungueltig waehlen" ///
		.m"keine andere Partei waehlen" ///
		.n"noch nicht entschieden" ///
		.o"nicht einzuschaetzen" ///
		.p"nicht bekannt", modify
		}
	else {
		replace `var'=".a keine Angabe" if `var'=="-99 keine Angabe"
		replace `var'=".a keine Angabe" if `var'=="-99"
		replace `var'=".c trifft nicht zu" if `var'=="-97 trifft nicht zu"
		replace `var'=".c trifft nicht zu" if `var'=="-97"
		replace `var'=".e nicht teilgenommen" if `var'=="-95 nicht teilgenommen"
		replace `var'=".e nicht teilgenommen" if `var'=="-95"
		replace `var'=".g Interview abgebrochen" if `var'=="-93 Interview abgebrochen"
		replace `var'=".g Interview abgebrochen" if `var'=="-93"
		}
}

foreach var of newlist lbl_wahlkreise  parteimissing flagnach {
			label define `var' ///
				.a"keine Angabe" ///
				.b"weiss nicht" ///
				.c"trifft nicht zu" ///
				.d"Split" ///
				.e"nicht teilgenommen" ///
				.f"nicht in Auswahlgesamtheit" ///
				.g"Interview abgebrochen" ///
				.h"Fehler in Daten" ///
				.i"nicht wahlberechtigt" ///
				.j"nicht waehlen" ///
				.k"keine Erst-/Zweitstimme abgegeben" ///
				.l"ungueltig waehlen" ///
				.m"keine andere Partei waehlen" ///
				.n"noch nicht entschieden" ///
				.o"nicht einzuschaetzen" ///
				.p"nicht bekannt", modify
}

***Coding of overall relevant variables according to Appendix A

**Turnout (Five-point scale)
*2009
tab1 a_kp?_170 a_kp7_180

foreach welle of numlist 1/6 {
recode a_kp`welle'_170 (1 6=1) (2=0.75) (3=0.5) (4=0.25) (5=0) (98=0.5) (else=.), gen(a_beteil`welle')  
   }
   
replace a_beteil1 = a_beteil2 if a_w1==0
   
tab a_kp3_170 a_beteil3, m
   
gen a_beteil7=1 if a_kp7_180==1 | a_kp7_180==6
replace a_beteil7=0 if a_kp7_180==2 

tab a_beteil7 a_kp7_180, m

* Integrate postal voters: after postal vote constant to 1
foreach welle of numlist 4/7 {  
replace a_beteil`welle' = 1 if a_kp3_170==6
}

foreach welle of numlist 5/7 {  
replace a_beteil`welle' = 1 if a_kp4_170==6
}

foreach welle of numlist 6/7 {  
replace a_beteil`welle' = 1 if a_kp5_170==6
}
   
foreach welle of numlist 7/7 {  
replace a_beteil`welle' = 1 if a_kp6_170==6
}   
*   
fre a_beteil?    



*2013
tab1 b_kp?_170 b_kp7_180

foreach welle of numlist 1/6 {
recode b_kp`welle'_170 (1 6=1) (2=0.75) (3=0.5) (4=0.25) (5=0) (98=0.5) (else=.), gen(b_beteil`welle')  
   }
   
replace b_beteil1 = b_beteil2 if b_w1b==0
   
tab b_kp3_170 b_beteil3, m
   
gen b_beteil7=1 if b_kp7_180==1 | b_kp7_180==6
replace b_beteil7=0 if b_kp7_180==2 

tab b_beteil7 b_kp7_180, m


* Integrate postal voters: after postal vote constant to 1
foreach welle of numlist 4/7 {  
replace b_beteil`welle' = 1 if b_kp3_170==6
}

foreach welle of numlist 5/7 {  
replace b_beteil`welle' = 1 if b_kp4_170==6
}

foreach welle of numlist 6/7 {  
replace b_beteil`welle' = 1 if b_kp5_170==6
}
   
foreach welle of numlist 7/7 {  
replace b_beteil`welle' = 1 if b_kp6_170==6
}   
*   
fre b_beteil?    



**Satisfaction with democracy
tab1 ?_kp?_020

*2009
foreach num of numlist 1 3 5 7 {
	gen a_zufr`num' = a_kp`num'_020
	recode a_zufr`num' (99=.)
	replace a_zufr`num' = (a_zufr`num'-5)/(-4)
}

* Replace for W2-starters
replace a_zufr1 = a_zufr3 if a_w1==0


*2013

foreach num of numlist 1 7 {
	gen b_zufr`num' = b_kp`num'_020
	recode b_zufr`num' (99=.)
	replace b_zufr`num' = (b_zufr`num'-5)/(-4)
}


** Indifference and Alienation

*2009

local suffix "a b c d e f"			//a=cdu, b=csu, c=spd, d=fdp, e=gru f=linke, 
foreach num1 of numlist 1/7 {
		foreach x of local suffix {
			gen a_m_skal_part_`x'`num1'=.
			replace a_m_skal_part_`x'`num1'=(a_kp`num1'_430`x'-1)/10
			replace a_m_skal_part_`x'`num1'=. if a_kp`num1'_430`x'==99
			replace a_m_skal_part_`x'`num1'=round(a_m_skal_part_`x'`num1',.1)
			}
		}


* Evaluation scale Union (CDU/CSU)
foreach num1 of numlist 1/7 {
		gen a_m_skal_part_union`num1'=.
		replace a_m_skal_part_union`num1'= a_m_skal_part_a`num1' if a_fedstate!=2
			replace a_m_skal_part_union`num1' = a_m_skal_part_b`num1' if a_fedstate==2 
	}
	
* Labels evaluation scales
foreach num1 of numlist 1/7 {
	lab var a_m_skal_part_a`num1' "Skalometer CDU"
	lab var a_m_skal_part_b`num1' "Skalometer CSU"
	lab var a_m_skal_part_c`num1' "Skalometer SPD,"
	lab var a_m_skal_part_d`num1' "Skalometer FDP,"	
	lab var a_m_skal_part_e`num1' "Skalometer Bündnis 90/Die Grünen"
	lab var a_m_skal_part_f`num1' "Skalometer Die Linke"
	lab var a_m_skal_part_union`num1' "Skalometer Union"
	}
* What is the maximum scale value?
foreach num1 of numlist 1/7 {	
egen a_m_maxskalo`num1'=rowmax(a_m_skal_part_union`num1' a_m_skal_part_c`num1' a_m_skal_part_d`num1' a_m_skal_part_e`num1' a_m_skal_part_f`num1')
tab a_m_maxskalo`num1', m
}

** Which party has the maximum scale value?
*Union
foreach num1 of numlist 1/7 {
gen a_m_maxunion`num1'=0
replace a_m_maxunion`num1'=1 if a_m_maxskalo`num1'==a_m_skal_part_union`num1' & a_m_maxskalo`num1'!=.
lab var a_m_maxunion`num1' "Maximalwert ist Union"
tab a_m_maxskalo`num1' a_m_skal_part_union`num1', m
}

*** c=spd, d=fdp, e=gru f=linke,
local suffix "c d e f"	
foreach num1 of numlist 1/7 {
foreach x of local suffix {
gen a_m_max`x'`num1'=0
replace a_m_max`x'`num1'=1 if a_m_maxskalo`num1'==a_m_skal_part_`x'`num1' & a_m_maxskalo`num1'!=.
tab a_m_max`x'`num1' a_m_skal_part_`x'`num1', m
}
}

* How high is the second evaluation scale value of the parties?
foreach num1 of numlist 1/7 {	
egen a_m_max2skalo_u`num1'=rowmax(a_m_skal_part_c`num1' a_m_skal_part_d`num1' a_m_skal_part_e`num1' a_m_skal_part_f`num1') if a_m_maxunion`num1'==1
egen a_m_max2skalo_c`num1'=rowmax(a_m_skal_part_union`num1' a_m_skal_part_d`num1' a_m_skal_part_e`num1' a_m_skal_part_f`num1') if a_m_maxc`num1'==1
egen a_m_max2skalo_d`num1'=rowmax(a_m_skal_part_union`num1' a_m_skal_part_c`num1' a_m_skal_part_e`num1' a_m_skal_part_f`num1') if a_m_maxd`num1'==1
egen a_m_max2skalo_e`num1'=rowmax(a_m_skal_part_union`num1' a_m_skal_part_d`num1' a_m_skal_part_c`num1' a_m_skal_part_f`num1') if a_m_maxe`num1'==1
egen a_m_max2skalo_f`num1'=rowmax(a_m_skal_part_union`num1' a_m_skal_part_d`num1' a_m_skal_part_c`num1' a_m_skal_part_e`num1') if a_m_maxf`num1'==1
}

* Measure of indifference
foreach num1 of numlist 1/7 {	
gen a_indiff`num1'=1-(a_m_maxskalo`num1'-a_m_max2skalo_u`num1') if a_m_maxunion`num1'==1
replace a_indiff`num1'= 1-(a_m_maxskalo`num1'-a_m_max2skalo_c`num1') if a_m_maxc`num1'==1
replace a_indiff`num1'= 1-(a_m_maxskalo`num1'-a_m_max2skalo_d`num1') if a_m_maxd`num1'==1
replace a_indiff`num1'= 1-(a_m_maxskalo`num1'-a_m_max2skalo_e`num1') if a_m_maxe`num1'==1
replace a_indiff`num1'= 1-(a_m_maxskalo`num1'-a_m_max2skalo_f`num1') if a_m_maxf`num1'==1
replace a_indiff`num1'=round(a_indiff`num1',.1)
tab a_indiff`num1', m
}



*******************
*** Alienation ***
*******************
egen a_maxskalon1=rowmax(a_m_skal_part_union1 a_m_skal_part_c1 a_m_skal_part_d1 a_m_skal_part_e1 a_m_skal_part_f1)
egen a_maxskalon2=rowmax(a_m_skal_part_union2 a_m_skal_part_c2 a_m_skal_part_d2 a_m_skal_part_e2 a_m_skal_part_f2)
egen a_maxskalon3=rowmax(a_m_skal_part_union3 a_m_skal_part_c3 a_m_skal_part_d3 a_m_skal_part_e3 a_m_skal_part_f3)
egen a_maxskalon4=rowmax(a_m_skal_part_union4 a_m_skal_part_c4 a_m_skal_part_d4 a_m_skal_part_e4 a_m_skal_part_f4)
egen a_maxskalon5=rowmax(a_m_skal_part_union5 a_m_skal_part_c5 a_m_skal_part_d5 a_m_skal_part_e5 a_m_skal_part_f5)
egen a_maxskalon6=rowmax(a_m_skal_part_union6 a_m_skal_part_c6 a_m_skal_part_d6 a_m_skal_part_e6 a_m_skal_part_f6)
egen a_maxskalon7=rowmax(a_m_skal_part_union7 a_m_skal_part_c7 a_m_skal_part_d7 a_m_skal_part_e7 a_m_skal_part_f7)

generate a_entfremda1=(a_maxskalon1-1)*-1
generate a_entfremda2=(a_maxskalon2-1)*-1
generate a_entfremda3=(a_maxskalon3-1)*-1
generate a_entfremda4=(a_maxskalon4-1)*-1
generate a_entfremda5=(a_maxskalon5-1)*-1
generate a_entfremda6=(a_maxskalon6-1)*-1
generate a_entfremda7=(a_maxskalon7-1)*-1
tab1 a_entfremda1-a_entfremda7 


foreach num of numlist 1/7{
lab var a_entfremda`num' "Entfremdung"
}

drop a_maxskalon*



*2013


*** Employ the evaluation scales of the parties
local suffix "a b c d e f h i"			//a=cdu, b=csu, c=spd, d=fdp, e=gru f=linke, h=piraten. i=afd
foreach num1 of numlist 1/7 {
		foreach x of local suffix {
			gen b_m_skal_part_`x'`num1'=.
			replace b_m_skal_part_`x'`num1'=(b_kp`num1'_430`x'-1)/10
			replace b_m_skal_part_`x'`num1'=round(b_m_skal_part_`x'`num1',.1)
			}
		}


* Evaluation scale Union
foreach num1 of numlist 1/7 {
		gen b_m_skal_part_union`num1'=.
		replace b_m_skal_part_union`num1'= b_m_skal_part_a`num1' if b_kpx_2601!=9
			replace b_m_skal_part_union`num1' = b_m_skal_part_b`num1' if b_kpx_2601==9 
	}
	
* label evaluation scale parties
foreach num1 of numlist 1/7 {
	lab var b_m_skal_part_a`num1' "Skalometer CDU"
	lab var b_m_skal_part_b`num1' "Skalometer CSU"
	lab var b_m_skal_part_c`num1' "Skalometer SPD,"
	lab var b_m_skal_part_d`num1' "Skalometer FDP,"	
	lab var b_m_skal_part_e`num1' "Skalometer Bündnis 90/Die Grünen"
	lab var b_m_skal_part_f`num1' "Skalometer Die Linke"
	lab var b_m_skal_part_union`num1' "Skalometer Union"
	lab var b_m_skal_part_h`num1' "Skalometer Die Piraten"
	lab var b_m_skal_part_i`num1' "Skalometer AFD"
	}

* What is the maximum evaluation scale value?

foreach num1 of numlist 1/7 {	
egen b_m_maxskalo`num1'=rowmax(b_m_skal_part_union`num1' b_m_skal_part_c`num1' b_m_skal_part_d`num1' b_m_skal_part_e`num1' b_m_skal_part_f`num1')
tab b_m_maxskalo`num1', m
}

** Which party has the maximum scale value?
*Union
foreach num1 of numlist 1/7 {
gen b_m_maxunion`num1'=0
replace b_m_maxunion`num1'=1 if b_m_maxskalo`num1'==b_m_skal_part_union`num1' & b_m_maxskalo`num1'!=.
lab var b_m_maxunion`num1' "Maximalwert ist Union"
tab b_m_maxskalo`num1' b_m_skal_part_union`num1', m
}

*** c=spd, d=fdp, e=gru f=linke,
local suffix "c d e f"	
foreach num1 of numlist 1/7 {
foreach x of local suffix {
gen b_m_max`x'`num1'=0
replace b_m_max`x'`num1'=1 if b_m_maxskalo`num1'==b_m_skal_part_`x'`num1' & b_m_maxskalo`num1'!=.
tab b_m_max`x'`num1' b_m_skal_part_`x'`num1', m
}
}
* How high is the second evaluation scale value of the parties?
foreach num1 of numlist 1/7 {	
egen b_m_max2skalo_u`num1'=rowmax(b_m_skal_part_c`num1' b_m_skal_part_d`num1' b_m_skal_part_e`num1' b_m_skal_part_f`num1') if b_m_maxunion`num1'==1
egen b_m_max2skalo_c`num1'=rowmax(b_m_skal_part_union`num1' b_m_skal_part_d`num1' b_m_skal_part_e`num1' b_m_skal_part_f`num1') if b_m_maxc`num1'==1
egen b_m_max2skalo_d`num1'=rowmax(b_m_skal_part_union`num1' b_m_skal_part_c`num1' b_m_skal_part_e`num1' b_m_skal_part_f`num1') if b_m_maxd`num1'==1
egen b_m_max2skalo_e`num1'=rowmax(b_m_skal_part_union`num1' b_m_skal_part_d`num1' b_m_skal_part_c`num1' b_m_skal_part_f`num1') if b_m_maxe`num1'==1
egen b_m_max2skalo_f`num1'=rowmax(b_m_skal_part_union`num1' b_m_skal_part_d`num1' b_m_skal_part_c`num1' b_m_skal_part_e`num1') if b_m_maxf`num1'==1
}

* Measure of indifference
foreach num1 of numlist 1/7 {	
gen b_indiff`num1'=1-(b_m_maxskalo`num1'-b_m_max2skalo_u`num1') if b_m_maxunion`num1'==1
replace b_indiff`num1'= 1-(b_m_maxskalo`num1'-b_m_max2skalo_c`num1') if b_m_maxc`num1'==1
replace b_indiff`num1'= 1-(b_m_maxskalo`num1'-b_m_max2skalo_d`num1') if b_m_maxd`num1'==1
replace b_indiff`num1'= 1-(b_m_maxskalo`num1'-b_m_max2skalo_e`num1') if b_m_maxe`num1'==1
replace b_indiff`num1'= 1-(b_m_maxskalo`num1'-b_m_max2skalo_f`num1') if b_m_maxf`num1'==1
replace b_indiff`num1'=round(b_indiff`num1',.1)
tab b_indiff`num1', m
}

foreach num of numlist 1/7{
lab var b_indiff`num' "Indifferenz"
}

 drop b_m_max*

******************
*** Alienation **
******************
egen b_maxskalon1=rowmax(b_m_skal_part_union1 b_m_skal_part_c1 b_m_skal_part_d1 b_m_skal_part_e1 b_m_skal_part_f1)
egen b_maxskalon2=rowmax(b_m_skal_part_union2 b_m_skal_part_c2 b_m_skal_part_d2 b_m_skal_part_e2 b_m_skal_part_f2)
egen b_maxskalon3=rowmax(b_m_skal_part_union3 b_m_skal_part_c3 b_m_skal_part_d3 b_m_skal_part_e3 b_m_skal_part_f3)
egen b_maxskalon4=rowmax(b_m_skal_part_union4 b_m_skal_part_c4 b_m_skal_part_d4 b_m_skal_part_e4 b_m_skal_part_f4)
egen b_maxskalon5=rowmax(b_m_skal_part_union5 b_m_skal_part_c5 b_m_skal_part_d5 b_m_skal_part_e5 b_m_skal_part_f5)
egen b_maxskalon6=rowmax(b_m_skal_part_union6 b_m_skal_part_c6 b_m_skal_part_d6 b_m_skal_part_e6 b_m_skal_part_f6)
egen b_maxskalon7=rowmax(b_m_skal_part_union7 b_m_skal_part_c7 b_m_skal_part_d7 b_m_skal_part_e7 b_m_skal_part_f7)

generate b_entfremda1=(b_maxskalon1-1)*-1
generate b_entfremda2=(b_maxskalon2-1)*-1
generate b_entfremda3=(b_maxskalon3-1)*-1
generate b_entfremda4=(b_maxskalon4-1)*-1
generate b_entfremda5=(b_maxskalon5-1)*-1
generate b_entfremda6=(b_maxskalon6-1)*-1
generate b_entfremda7=(b_maxskalon7-1)*-1
tab1 b_entfremda1-b_entfremda7


foreach num of numlist 1/7{
lab var b_entfremda`num' "Entfremdung"
}




**Political interest
*2009
foreach num1 of numlist 2 4 6 7 {
		gen a_polint`num1'=a_kp`num1'_010
			recode a_polint`num1' (99=.)
			replace a_polint`num1'= (a_polint`num1'-5)/(-4)
	}
	
*label political interest in general
foreach num1 of numlist 2 4 6 7 {
	lab var a_polint`num1' "Politisches Interesse allgemein"
}


*2013

foreach num1 of numlist 1/7  {
	gen b_polint`num1'=.
replace b_polint`num1'= (b_kp`num1'_010-5)/(-4)
	lab var b_polint`num1' "Politisches Interesse allgemein"
	}



save "data2009_2013 participants 7 waves.dta", replace
